package com.ckf.mongodb_punch.controller;

import com.ckf.mongodb_punch.entity.Attend;
import com.ckf.mongodb_punch.service.AttendService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class AttendController {

    private Logger log = LoggerFactory.getLogger(AttendController.class);

    @Autowired
    private AttendService attendService;

    /**
     * 签到
     * @param name
     * @return
     */
    @GetMapping("/sign")
    public String sign(String name){
        /**
         * 将名字传给服务层，mongodb修改登录状态
         */
        attendService.sign(name);
        System.out.println("签到成功");
        return "ok";
    }

    /**
     * 上传文件
     * @param classes
     * @param nameListExcel
     * @return
     */
    @PostMapping("/upload")
    public String upload(String classes, MultipartFile nameListExcel){
        /**
         * 接收到前台传过来的文件对象，交给service层或者Excel工具类来解析数据
         * System.out.println("接收前台表单提交数据："+classes+nameListExcel);
         */
        String result = attendService.upload(classes,nameListExcel);
        return result;
    }


    /**
     *  分页查询学生信息
     */
    @GetMapping("/find")
    public Map find(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        System.out.println("-----------------进入学生全查询模式--------------------");
        Map outMap = new HashMap<String,Object>();
        PageHelper.startPage(page, limit);
        List<Attend> list = attendService.findAllBySign(0);

        if (list != null) {
            outMap.put("code", 0);
            System.out.println("查询成功");
            outMap.put("msg", "查询成功");
            log.info("查询成功");

            PageInfo pageInfo = new PageInfo(list);
            outMap.put("data", pageInfo.getList());
            outMap.put("count", pageInfo.getTotal());
        } else {
            outMap.put("code", 100);
            outMap.put("msg", "查询失败");
            log.info("查询失败");
        }

        return outMap;
    }

    /**
     * 查询未签到同学 和已签到同学
     * @return
     */
    @GetMapping("/list")
    public Map list(){
        Map result = new HashMap<String,Object>();
        /**
         * 已签到
         */
        List<Attend> complete = attendService.findAllBySign(1);
        result.put("complete",complete);

        /**
         * 未签到
         */
        List<Attend> incomplete = attendService.findAllBySign(0);
        result.put("incomplete",incomplete);
        return result;
    }
}
